package hibernate;

import java.util.List;

import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//import orm.Adminstab;

/**
 * A data access object (DAO) providing persistence and search support for
 * Adminstab entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see hibernate.Adminstab
 * @author MyEclipse Persistence Tools
 */
public class AdminstabDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(AdminstabDAO.class);
	// property constants
	public static final String ACCOUNT = "account";
	public static final String PWD = "pwd";
	public static final String NAME = "name";
	public static final String TEL_NUMBER = "telNumber";
	public static final String MAIL = "mail";

	public void save(Adminstab transientInstance) {
		log.debug("saving Adminstab instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Adminstab persistentInstance) {
		log.debug("deleting Adminstab instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Adminstab findById(java.lang.Integer id) {
		log.debug("getting Adminstab instance with id: " + id);
		try {
			Adminstab instance = (Adminstab) getSession().get(
					"hibernate.Adminstab", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Adminstab instance) {
		log.debug("finding Adminstab instance by example");
		try {
			List results = getSession().createCriteria("hibernate.Adminstab")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Adminstab instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Adminstab as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByAccount(Object account) {
		return findByProperty(ACCOUNT, account);
	}

	public List findByPwd(Object pwd) {
		return findByProperty(PWD, pwd);
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByTelNumber(Object telNumber) {
		return findByProperty(TEL_NUMBER, telNumber);
	}

	public List findByMail(Object mail) {
		return findByProperty(MAIL, mail);
	}

	public List findAll() {
		log.debug("finding all Adminstab instances");
		try {
			String queryString = "from Adminstab";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Adminstab merge(Adminstab detachedInstance) {
		log.debug("merging Adminstab instance");
		try {
			Adminstab result = (Adminstab) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Adminstab instance) {
		log.debug("attaching dirty Adminstab instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Adminstab instance) {
		log.debug("attaching clean Adminstab instance");
		try {
			getSession().buildLockRequest(LockOptions.NONE).lock(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	//根据用户名和密码匹配检查登录权限
	public Adminstab loginCheck(String name,String password){
		
		log.debug("finding all Adminstab instances");
		try {
			String hqlString = "from Adminstab as a where a.account=:p_account and a.pwd = :p_pwd";
			Query queryObject = getSession().createQuery(hqlString);
			queryObject.setString("p_account", name);
			queryObject.setString("p_pwd", password);
			List<Adminstab> list = queryObject.list();
			if(list !=null && list.size() > 0){               
				return list.get(0);
			}else {
				return null;
			}
		} catch (RuntimeException re) {                       
			log.error("find  failed", re);
			throw re;
		}
	}
	
	//用户名存在检查（Ajax用户名重名检查）
	public boolean accountCheck(String name){
		log.debug("finding  Adminstab instances");
		try {
			String hqlString = "from Adminstab as a where a.account=:p_account";
			Query queryObject = getSession().createQuery(hqlString);
			queryObject.setString("p_account", name);
			List<Adminstab> list = queryObject.list();
			if(list !=null && list.size() > 0){               
				return false;
			}else {
				return true;                                  
			}
		} catch (RuntimeException re) {                       
			log.error("find  failed", re);
			throw re;
		}
	}
}
